package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    //注入JedisPool
    @Autowired
    private JedisPool jedisPool;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页显示多少条
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();

        //分页插件助手
        PageHelper.startPage(currentPage,pageSize);
        //调用Dao层,查找数据
        Page<Setmeal> setmealPage = setmealDao.findByCondition(queryString);

        //获取数据
        List<Setmeal> result = setmealPage.getResult();
        //获得总条数
        long total = setmealPage.getTotal();
        return new PageResult(total,result);
    }

    //添加套餐
    @Override
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        //获取img，存入redis中，用于查询差集，删除垃圾文件
        String img = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        //调用Dao层进行添加  获取主键自增返回的值
        setmealDao.add(setmeal);
        //返回自增的ID 用于在中间表里添加套餐对应检查组信息
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckGroup(setmealId,checkGroupIds);

    }

    //查询全部套餐
    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(String id) {
        return setmealDao.findById(id);
    }


    //查询套餐出现的次数
    @Override
    public Result findSetmealCount() {
        //最终返回的map集合 里面包含setmealNames[name],setmealCount{name,value}
        Map<String, Object> resultMap = new HashMap<>();
        //创建套餐名字的集合
        List<String> setmealNames = new ArrayList<>();
        //创建套餐名和套餐预约次数的集合
        List<Map> setmealCount = setmealDao.findSetmealCountAndName();
        //循环遍历,拿出name,把name赋值给setmealNames
        for (Map map : setmealCount) {
            String name = (String) map.get("name");
            setmealNames.add(name);
        }

        resultMap.put("setmealNames", setmealNames);
        resultMap.put("setmealCount", setmealCount);
        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, resultMap);
    }


    //设置检查组和套餐的关联关系
    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkGroupIds) {
        //判断Ids有没有数据
        if (checkGroupIds != null || checkGroupIds.length > 0){
            //循环遍历数组，往中间表添加数据
            for (Integer checkGroupId : checkGroupIds) {
                //创建一个map数组，parameterType 是一个map
                Map<String,Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("setmealId",setmealId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
